﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Tesla.Service.DataContracts;
using Tesla.DataAccess.DataAccessComponents;
using Tesla.Util.Utilities;
using Tesla.DataAccess.DataAccessLogic.Interfaces;

namespace Tesla.DataAccess.DataAccessLogic.Implementations
{
    public class TaskDA : BaseOperationalDA, ITaskDA
    {
        #region BaseOperationalDA Members
        public override int? Create(BaseDC bDC)
        {
            int success = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;

                    success = context.usp_CreateTask(
                                                SetSafeString(((TaskDC)bDC).TaskCode),
                                                SetSafeInt(((TaskDC)bDC).TaskTypeID),
                                                SetSafeInt(((TaskDC)bDC).PhaseID),
                                                SetSafeString(((TaskDC)bDC).TaskDescription),
                                                SetSafeInt(((TaskDC)bDC).Task_PlannedEffort),
                                                SetSafeInt(((TaskDC)bDC).Task_AdditionalEffort),
                                                SetSafeInt(((TaskDC)bDC).ParentTaskID));


                    context.SubmitChanges();
                    return success;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override int Update(BaseDC bDC)
        {
            int recordCount = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    recordCount = context.usp_UpdateTask(
                                                SetSafeInt(((TaskDC)bDC).TaskID),
                                                SetSafeString(((TaskDC)bDC).TaskCode),
                                                SetSafeInt(((TaskDC)bDC).TaskTypeID),
                                                SetSafeInt(((TaskDC)bDC).PhaseID),
                                                SetSafeString(((TaskDC)bDC).TaskDescription),
                                                SetSafeInt(((TaskDC)bDC).Task_PlannedEffort),
                                                SetSafeInt(((TaskDC)bDC).Task_AdditionalEffort),
                                                SetSafeInt(((TaskDC)bDC).ParentTaskID));
                    context.SubmitChanges();
                    return recordCount;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> tasks = null;
            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_DisplayAllTaskResult> taskList = context.usp_DisplayAllTask(gridSize, ref totalRecordCount).Cast<usp_DisplayAllTaskResult>();
                    tasks = GenericConversion.ConvertToTargetType(taskList.ToList<usp_DisplayAllTaskResult>().ToArray(), typeof(usp_DisplayAllTaskResult), typeof(TaskDC));

                }

                return tasks;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> tasks = null;

            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_SearchTaskByNameResult> taskList = context.usp_SearchTaskByName(name, gridSize, ref totalRecordCount).Cast<usp_SearchTaskByNameResult>();
                    tasks = GenericConversion.ConvertToTargetType(taskList.ToList<usp_SearchTaskByNameResult>().ToArray(), typeof(usp_SearchTaskByNameResult), typeof(TaskDC));
                }
                return tasks;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> Read(int id)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadTaskResult> taskList= context.usp_ReadTask(id).Cast<usp_ReadTaskResult>();
                    return GenericConversion.ConvertToTargetType(taskList.ToList<usp_ReadTaskResult>().ToArray(), typeof(usp_ReadTaskResult), typeof(TaskDC));                   
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadTaskByRowNumberRangeResult> taskList = context.usp_ReadTaskByRowNumberRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString).Cast<usp_ReadTaskByRowNumberRangeResult>();
                    return GenericConversion.ConvertToTargetType(taskList.ToList<usp_ReadTaskByRowNumberRangeResult>().ToArray(), typeof(usp_ReadTaskByRowNumberRangeResult), typeof(TaskDC));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        public IList<BaseDC> ReadAllTasks(int taskID)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadAllTasksResult> taskList = context.usp_ReadAllTasks(taskID).Cast<usp_ReadAllTasksResult>();
                    return GenericConversion.ConvertToTargetType(taskList.ToList<usp_ReadAllTasksResult>().ToArray(), typeof(usp_ReadAllTasksResult), typeof(TaskDC));

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadAllTaskTypes()
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadAllTaskTypesResult> taskTypesList = context.usp_ReadAllTaskTypes().Cast<usp_ReadAllTaskTypesResult>();
                    return GenericConversion.ConvertToTargetType(taskTypesList.ToList<usp_ReadAllTaskTypesResult>().ToArray(), typeof(usp_ReadAllTaskTypesResult), typeof(TaskTypeDC));

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadAllPhases()
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadAllPhasesResult> phaseList = context.usp_ReadAllPhases().Cast<usp_ReadAllPhasesResult>();
                    return GenericConversion.ConvertToTargetType(phaseList.ToList<usp_ReadAllPhasesResult>().ToArray(), typeof(usp_ReadAllPhasesResult), typeof(PhaseDC));

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
